package com.wc.dao.impl;

import com.wc.dao.DeptDao;
import com.wc.dao.EmpDao;
import com.wc.domain.Dept;
import com.wc.domain.Emp;
import com.wc.fake.DB;
import com.wc.utils.Md5Util;

/**
 * @author wanghao
 */
public class EmpDaoImpl implements EmpDao {
    private DeptDao deptDao = new DeptDaoImpl();

    @Override
    public String selectSaltByEmpno(Integer empno) {
        Emp emp = selectEmpByEmpno(empno);
        if (emp != null) {
            return emp.getSalt();
        }
        return null;
    }

    @Override
    public Emp selectEmpByEmpno(Integer empno) {
        for (Emp emp : DB.emps) {
            if (emp.getEmpno().equals(empno)) {
                return emp;
            }
        }
        return null;
    }

    @Override
    public int selectEmpnoByAccount(String account) {
        for (Emp emp : DB.emps) {
            if (emp.getAccount().equals(account)) {
                return emp.getEmpno();
            }
        }
        return -1;
    }

    @Override
    public boolean updateEmp(Emp emp) {
        if (emp == null) {
            return false;
        }
        for (int i = 0; i < DB.emps.size(); i++) {
            Emp tmp = DB.emps.get(i);
            if (tmp.getEmpno().equals(emp.getEmpno())) {
                if (emp.getEname() != null) {
                    tmp.setEname(emp.getEname());
                }
                if (emp.getJob() != null) {
                    tmp.setJob(emp.getJob());
                }
                if (emp.getMgr() != null) {
                    tmp.setMgr(emp.getMgr());
                }
                if (emp.getHiredate() != null) {
                    tmp.setHiredate(emp.getHiredate());
                }
                if (emp.getSal() != null) {
                    tmp.setSal(emp.getSal());
                }
                if (emp.getComm() != null) {
                    tmp.setComm(emp.getComm());
                }
                if (emp.getDeptno() != null) {
                    tmp.setDeptno(emp.getDeptno());
                }
                if (emp.getAccount() != null) {
                    tmp.setAccount(emp.getAccount());
                }
                if (emp.getPassword() != null) {
                    //修改盐值
                    if(emp.getSalt() != null) {
                        tmp.setPassword(Md5Util.encode(emp.getPassword(),emp.getSalt()));
                    }else{
                        tmp.setPassword(Md5Util.encode(emp.getPassword(),tmp.getSalt()));
                    }
                }
                if (emp.getSalt() != null) {
                    tmp.setSalt(emp.getSalt());
                }
                if (emp.getRole() != null) {
                    tmp.setRole(emp.getRole());
                }
                return true;
            }
        }
        return false;
    }

    @Override
    public Dept selectDeptByEmpno(Integer empno) {
        Emp emp = selectEmpByEmpno(empno);
        if (emp != null) {
            Integer deptno = emp.getDeptno();
            Dept dept = deptDao.selectDeptByDeptno(deptno);
            return dept;
        }
        return null;
    }

}